package com.example.RxJavaDemo;


import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.example.RxJavaDemo.databinding.ActivityMainBinding;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import io.reactivex.Observable;


public class MainActivity extends BaseActivity<ActivityMainBinding> {

    private final String TAG = this.getClass().getName();

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_main;
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initView(Bundle savedInstanceState) {
    /* RxView.clicks(mBinding.btnStart)
                .throttleFirst(3000, TimeUnit.MILLISECONDS)
                .subscribe(aVoid -> showToast("3秒点一次！"));*/
        mBinding.btnStart.setOnClickListener(v -> {
            toDoSomething();
        });
    }

    private void toDoSomething() {
        List<Student.Course> courses = new ArrayList<>();
        courses.add(new Student.Course("aa"));
        courses.add(new Student.Course("bb"));
        courses.add(new Student.Course("aa"));
        Student s1 = new Student("1", "zhangsan", courses);
        Student s2 = new Student("2", "lisi", courses);
        Student[] students = {s1, s2};
        List<Student> studentList = Arrays.asList(s1, s2);

        Observable.fromIterable(courses)
                .map(course -> course.getCname())
                .distinct()
                .subscribe(s -> {
                    Log.e(TAG, "name:" + s);
                });



        /*Flowable.fromArray(students)
                .flatMap((Function<Student, Publisher<Student.Course>>) student -> Flowable.fromIterable(student.getList()))
                .subscribe(o -> {
                    Log.e(TAG, "name:" + o.getCname());
                });*/

      /*  Flowable.just(1.2, 3)
                .doOnNext((Consumer<Number>) number -> Log.e(TAG, "保存：" + number.toString()))
                .doAfterNext((Consumer<Number>) number -> Log.e(TAG, "保存After：" + number.toString()))
                .subscribe((Consumer<Number>) number -> Log.e(TAG, "number:" + number.toString()));*/

        /*Flowable.fromIterable(studentList)
                .flatMap((Function<Student, Flowable<Student.Course>>) student -> Flowable.fromIterable(student.getList()))
                .subscribe(course -> Log.e(TAG, "name:" + course.getCname()));
*/


//        rxjava1.0
       /* String url = "http://es-oss.oss-cn-shenzhen.aliyuncs.com/rombra/friends/1532069498782276300.png?Expires=1847429498&OSSAccessKeyId=LTAIwzZqzLuxYbWy&Signature=mnyGW1aZlZ3KfUFi31%2Bd5svE1DQ%3D";
        FileUtils utils = new FileUtils(this);
        utils.startDownload(url)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<Bitmap>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(Bitmap bitmap) {
                        mBinding.ivImage.setImageBitmap(bitmap);
                    }
                });*/
//        utils.startDownload(url, mBinding.ivImage);

        /*Observable.from(students)
                .map(student -> student.getName())
                .subscribe(s -> Log.e(TAG, "name:" + s));
*/
        /*Observable.from(students)
                .flatMap((Func1<Student, Observable<Student.Course>>) student -> Observable.from(student.getList()))
                .subscribe(course -> {
                    Log.e(TAG, "course:" + course.getCname());
                });
*/
        /*Observable.create((Observable.OnSubscribe<Drawable>) subscriber -> {
            subscriber.onNext(ContextCompat.getDrawable(MainActivity.this, R.mipmap.ic_launcher_round));
            subscriber.onCompleted();
        }).subscribe(new Subscriber<Drawable>() {
            @Override
            public void onCompleted() {
                Toast.makeText(MainActivity.this, "onCompleted!", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onError(Throwable e) {
                Toast.makeText(MainActivity.this, "Error!", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNext(Drawable drawable) {
                mBinding.ivImage.setImageDrawable(drawable);
            }
        });*/
    }

    private void showToast(String text) {
        Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT).show();
    }
}
